package stark.app.function.netusage;

import android.widget.ProgressBar;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.stark.netusage.lib.NetUsageInfo;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import stark.app.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;

/**
 * Author：daizhongbin on 2022/8/10
 * Email：1027154338@qq.com
 * Desc:
 */
public class NetUsageDateAdapter extends StkProviderMultiAdapter<NetUsageInfo> {

    private long mMaxMobileTotalBytes;
    private long mMaxWifiTotalBytes;

    public NetUsageDateAdapter() {
        super(1);
        addItemProvider(new MyItemProvider());
    }

    @Override
    protected int getDefItemCount() {
        findMaxMobileWifiTotalBytes(true);
        return super.getDefItemCount();
    }

    private String getDate(NetUsageInfo usageInfo) {
        return TimeUtils.millis2String(usageInfo.starTime, "MM月dd日");
    }

    private void findMaxMobileWifiTotalBytes(boolean forceReFind) {
        if (!forceReFind) {
            if (mMaxMobileTotalBytes > 0 || mMaxWifiTotalBytes > 0) {
                return;
            }
        }
        List<NetUsageInfo> infos = getData();
        if (infos == null || infos.size() == 0) {
            return;
        }
        mMaxMobileTotalBytes = infos.get(0).mobileTotalBytes;
        mMaxWifiTotalBytes = infos.get(0).wifiTotalBytes;
        for (int i = 1; i < infos.size(); i++) {
            if (mMaxMobileTotalBytes < infos.get(i).mobileTotalBytes) {
                mMaxMobileTotalBytes = infos.get(i).mobileTotalBytes;
            }
            if (mMaxWifiTotalBytes < infos.get(i).wifiTotalBytes) {
                mMaxWifiTotalBytes = infos.get(i).wifiTotalBytes;
            }
        }
    }

    private class MyItemProvider extends BaseItemProvider<NetUsageInfo> {

        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_date_net_usage;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, NetUsageInfo usageInfo) {
            TextView tvDate = holder.getView(R.id.tvDate);
            tvDate.setText(getDate(usageInfo));

            String mobile = ConvertUtils.byte2FitMemorySize(usageInfo.mobileTotalBytes, 1);
            holder.setText(R.id.tvMobileData, mobile);
            int percent = (int) (usageInfo.mobileTotalBytes * 1f / mMaxMobileTotalBytes * 100);
            ProgressBar pbMobile = holder.getView(R.id.pbMobile);
            pbMobile.setProgress(percent);

            String wifi = ConvertUtils.byte2FitMemorySize(usageInfo.wifiTotalBytes, 1);
            holder.setText(R.id.tvWifiData, wifi);
            percent = (int) (usageInfo.wifiTotalBytes * 1f / mMaxWifiTotalBytes * 100);
            ProgressBar pbWifi = holder.getView(R.id.pbWifi);
            pbWifi.setProgress(percent);
        }
    }

}
